<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Api extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function getRankList() {
        $fbuser_id = $_REQUEST['fbuser_id'];
        $limit = $_REQUEST['limit'];
        $type = $_REQUEST['type'];
        $data = $this->friend->getRankList($fbuser_id, $limit, $type);

        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

    public function getProductRecommendList() {
        $limit = $_REQUEST['limit'];
        $type = 'books';
        if (isset($_REQUEST['type'])) {
            $type = $_REQUEST['type'];
        }
        $data = $this->product->getRecommendList($limit, $type);
        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

    public function addMonoList() {
        $user_id = $_REQUEST['fbuser_id'];
        $product_id = $_REQUEST['product_id'];
        $status = $_REQUEST['status'];
        $type = $_REQUEST['type'];
        if (!$this->product->issetProduct($user_id, $product_id)) {
            $this->product->addMonoList($user_id, $product_id, $status, $type);
        } else {
            $this->product->updateMonoList($user_id, $product_id, $status);
        }
        $data = array("message" => 1, "data" => "");
        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

    public function getMonoProductList() {
        $fbuser_id = $_REQUEST['fbuser_id'];
        $status = $_REQUEST['status'];
        $limit = $_REQUEST['limit'];
        $data = $this->product->getMonoList($fbuser_id, $status, $limit);
        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

    public function getHumanDetail() {
        header('Content-type: text/json');
        header('Content-type: application/json');
        $id = $_REQUEST['human_id'];
        $data = $this->friend->getFriendInfo($id);
        echo json_encode($data);
    }

    public function getMonoList() {
        $fbuser_id = $_REQUEST['fbuser_id'];
        $status = $_REQUEST['status'];
    }

    public function getMyListHuman() {
        $isFriend = "";
        $sex = "";
        $fbuser_id = $_REQUEST['fbuser_id'];
        $limit = $_REQUEST['limit'];
        $type = $_REQUEST['type'];
        $friendsString = "";
        if (isset($_REQUEST['orderby'])) {
            $orderby = $_REQUEST['orderby'];
        } else {
            $orderby = "ASC";
        }
        $friendids = $this->user->getAllFBFriends($fbuser_id, $this);
        $friendsString = array();
        foreach ($friendids as $item) {
            $friendsString[] = $item['uid'];
        }
        if (isset($_REQUEST['isfriend'])) {
            $isFriend = $_REQUEST['isfriend'];
            if (count($friendsString) > 0) {
                //echo $isFriend;
                if ($isFriend == 1) {
                    $friendsString = "IN (" . implode(",", $friendsString) . ")";
                } else {
                    $friendsString = "NOT IN (" . implode(",", $friendsString) . ")";
                }
            }
        }
        if (isset($_REQUEST['sex']))
            $sex = $_REQUEST['sex'];
        $data = $this->friend->getRankList($fbuser_id, $limit, $type, $sex, $friendsString, $orderby);

        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

    function getMyFavorite() {

        $fbuser_id = $_REQUEST['fbuser_id'];
        $data = $this->user->getFavorite($fbuser_id);
        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

    function getMyInfo() {
        $fbuser_id = $_REQUEST['fbuser_id'];
        $data = $this->user->getInfo($fbuser_id, $this);
        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

    function updateAboutme() {
        $aboutme = $_REQUEST['aboutme'];
        $fbuser_id = $_REQUEST['fbuser_id'];
        $data = $this->user->updateAboutMe($aboutme, $fbuser_id);
        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

    function addMyFavoriteRecommend() {
        header('Content-type: text/json');
        header('Content-type: application/json');
        $data = array();
        $fbuser_id = $_REQUEST['fbuser_id'];
        $user_id = $this->user->getIdOfUser($fbuser_id);
        $listFavorite = $this->user_favorite->getFavorite($user_id[0]->id);
        $fb_books = $listFavorite[0]->fb_books;
        $fb_movies = $listFavorite[0]->fb_movies;
        $fb_music = $listFavorite[0]->fb_music;
        $favorite_order = $this->user_favorite->getFavoriteOrder($user_id[0]->id);
        if (isset($_REQUEST['app_books']) && isset($_REQUEST['app_movies']) && isset($_REQUEST['app_music'])) {
            $app_books = $listFavorite[0]->app_books;
            if ($_REQUEST['app_books'] != "")
                $app_books = $app_books . "," . $_REQUEST['app_books'];

            $app_movies = $listFavorite[0]->app_movies;
            if ($_REQUEST['app_movies'] != "")
                $app_movies = $app_movies . "," . $_REQUEST['app_movies'];
            $app_music = $listFavorite[0]->app_music;
            if ($_REQUEST['app_music'] != "")
                $app_music = $app_music . "," . $_REQUEST['app_music'];
            $favorite_order->favorite_order_books = $_REQUEST['app_books'] . "," . $favorite_order->favorite_order_books;
            $favorite_order->favorite_order_movies = $_REQUEST['app_movies'] . "," . $favorite_order->favorite_order_movies;
            $favorite_order->favorite_order_music = $_REQUEST['app_music'] . "," . $favorite_order->favorite_order_music;



            $this->user_favorite->update_user($user_id[0]->id, $fb_books, $fb_movies, $fb_music, $app_books, $app_movies, $app_music, $favorite_order);
            $data['message'] = "success";
        }
        echo json_encode($data);
    }

    function addMyFavorite() {
        header('Content-type: text/json');
        header('Content-type: application/json');
        $data = array();
        try {
            if (isset($_REQUEST['app_books']) && isset($_REQUEST['app_books']) && isset($_REQUEST['app_books'])) {
                $app_books = $_REQUEST['app_books'];
                $app_movies = $_REQUEST['app_movies'];
                $app_music = $_REQUEST['app_music'];
                if (isset($_REQUEST['favorite_order_books']) && isset($_REQUEST['favorite_order_movies']) && isset($_REQUEST['favorite_order_music'])) {
                    $favorite_order->favorite_order_books = $_REQUEST['favorite_order_books'];
                    $favorite_order->favorite_order_movies = $_REQUEST['favorite_order_movies'];
                    $favorite_order->favorite_order_music = $_REQUEST['favorite_order_music'];
                } else {
                    $favorite_order = '';
                }

                //record fb change
                if (isset($_REQUEST['fb_favorite_change'])) {
                    $fb_favorite_change = $_REQUEST['fb_favorite_change'];
                    $fb_favorite_change = explode(",", $fb_favorite_change);
                    $this->user_favorite->insertFavoriteChange($fb_favorite_change);
                }
                //
                $fbuser_id = $_REQUEST['fbuser_id'];
                $user_id = $this->user->getIdOfUser($fbuser_id);
                $user_favorite = $this->user_favorite->getFavorite($user_id[0]->id);
                $fb_books = $user_favorite[0]->fb_books;
                $fb_movies = $user_favorite[0]->fb_movies;
                $fb_music = $user_favorite[0]->fb_music;
                $this->user_favorite->update_user($user_id[0]->id, $fb_books, $fb_movies, $fb_music, $app_books, $app_movies, $app_music, $favorite_order);
                $data['message'] = "success";
            } else {
                $data['message'] = "data update is wrong";
            }
        } catch (Exception $exc) {
            $data['message'] = $exc->getMessage();
        }
        echo json_encode($data);
    }

    function searchProducts() {
        $type_code = $_REQUEST['type_code'];
        $params = "";

        if ($type_code != "All") {
            $params .= "&type_code=" . $type_code;
            //echo $params;
        }
        if (isset($_REQUEST['genre'])&&$_REQUEST['genre']!="All") {
            $params .= "&genre1=" . $_REQUEST['genre'];
        }
        if (isset($_REQUEST['release_date'])&&$_REQUEST['release_date']!="All") {
            $params .= "&release_date=" . $_REQUEST['release_date'];
        }
        $config = $this->config->config;
        if (isset($_REQUEST['code'])) {
            $q = "code:" . $_REQUEST['code'];
            $q1 = "person_name:-1";
        }   
        if (isset($_REQUEST['name'])) {
           // echo urlencode($_REQUEST['name']);
            $q = "name:" . ($_REQUEST['name']);
            $q1 = "person_name:" . $_REQUEST['name'];
        }
//        if(isset($_REQUEST['person_name'])&&$_REQUEST['person_name']!="All"){
//            $q = "name:-1";
//            $q1 = "person_name:" . $_REQUEST['person_name'];
//        }
        $offset = $_REQUEST['offset'] * 10;
        $params .= "&rows=" . $offset;
        $searchurl = $config['searchurl'] . "?q=" . urlencode($q) . $params;
        $searchurl1 = $config['searchurl'] . "?q=" . urlencode($q1) . $params;
        $resultName = json_decode(file_get_contents($searchurl))->response;
        $resultAuthor = json_decode(file_get_contents($searchurl1))->response;
        $resultNameArray = $resultName->docs;
        $resultAuthorArray = $resultAuthor->docs;

        $result = array();
        foreach ($resultNameArray as $value) {

            $check = 0;
            foreach ($result as $item) {
                if ($item->isbn == $value->isbn) {
                    $check = 1;
                    break;
                }
            }
            if ($check == 0) {
                $result[] = $value;
            }
            if (count($result) >= $offset) {
                break;
            }
        }
        if (count($result) < $offset) {
            foreach ($resultAuthorArray as $value) {
                $check = 0;
                foreach ($result as $item) {
                    if ($item->isbn == $value->isbn) {
                        $check = 1;
                        break;
                    }
                }
                if ($check == 0) {
                    $result[] = $value;
                }
                if (count($result) >= $offset) {
                    break;
                }
            }
        }
        $data = array();
        $genre = array();
        $person = array();
        foreach ($result as $item) {
            if (isset($_REQUEST['person_name'])&&$_REQUEST['person_name']!="All") {
                if (isset($item->person_name)) {
                    if (!in_array($_REQUEST['person_name'], $item->person_name)) {
                        continue;
                    }
                }
            }
            //create product object
            $product = new stdClass();
            $product->type_code = $item->type_code;
            $product->ctrl = $item->control;
            $product->product_id = $item->isbn;
            $product->product_name = $item->name;
            $product->rate_score = $item->score;
            if(isset($item->person_name))
                $product->anotherinfo = implode(",",$item->person_name);
            else
                $product->anotherinfo = "";
            $product->price = $item->price;
            $product->maker = $item->publisher_name[0];
            $product->pop = $item->isbn;
            if (isset($item->review)) {
                $product->review = $item->review;
            } else {
                $product->review = "";
            }

            if ($item->type_code == "BK") {
                $product->image_link = "http://boox.jp/image/BK/M/0/" . $product->product_id . ".jpg";
            }
            if ($item->type_code == "C") {
                $product->image_link = "http://boox.jp/image/C/M/0/" . $product->product_id . ".jpg";
            }
            if ($item->type_code == "D") {
                $product->image_link = "http://boox.jp/image/D/M/0/" . $product->product_id . ".jpg";
            }
            $data["data"][] = $product;
            //end create product object
            //get genre array
            foreach ($item->genre as $itemGenre) {
                if (!in_array($itemGenre, $genre)) {
                    $genre[] = $itemGenre;
                }
            }
            if (isset($item->person_name)) {
                foreach ($item->person_name as $itemPerson) {
                    if (!in_array($itemPerson, $person)) {
                        $person[] = $itemPerson;
                    }
                }
            }
            //end get genre array
        }
        $data['genre'] = $genre;
        $data['person'] = $person;
        $data['total'] = $resultName->numFound + $resultAuthor->numFound;
        header('Content-type: text/json');
        header('Content-type: application/json');
        echo json_encode($data);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */